<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>销售策略配赠查看</title>
    <script type="text/javascript">
      //总配赠，总消费，余款
      Ext.onReady(function(){
        //总配赠
        var sumGrant = Ext.create('Ext.form.field.Number', {
          fieldLabel: '总配赠款',
          name: 'sum_grant',
          allowBlank: false,
          readOnly:true,
          anchor: '100%'
        });
        
        //总配赠消费
        var sumGrantConsume = Ext.create('Ext.form.field.Number', {
          fieldLabel: '配赠总消费',
          name: 'sum_grant_consume',
          allowBlank: false,
          readOnly:true,
          anchor: '100%'
        });

        //配赠余款
        var grantResidualPayment = Ext.create('Ext.form.field.Number', {
          fieldLabel: '配赠余款',
          allowBlank: false,
          name: 'grant_residual_payment',
          readOnly:true,
          anchor: '100%'
        });

        //总预收
        var sumReceivable = Ext.create('Ext.form.field.Number', {
          fieldLabel: '总收款',
          name: 'sum_receivable_grant',
          allowBlank: false,
          readOnly:true,
          anchor: '100%'
        });

        //总配赠消费
        var sumReceivableConsume = Ext.create('Ext.form.field.Number', {
          fieldLabel: '收款总消费',
          name: 'sum_receivable_consume',
          allowBlank: false,
          readOnly:true,
          anchor: '100%'
        });

        //收款余款
        var receivableResidualPayment = Ext.create('Ext.form.field.Number', {
          fieldLabel: '收款余款',
          allowBlank: false,
          name: 'receivable_residual_payment',
          readOnly:true,
          anchor: '100%'
        });
        
        //暂时保存按钮
        var closeButton = Ext.create('Ext.Button', {
          text: '关闭',
          tooltip: '关闭当前窗口。',
          iconCls: 'exit',
          handler: function(){
            viewSalesStrategyRdsidualPaymentWin.destroy();
          }
        });
        
        var residualPaymentFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 65,
            labelAlign: 'right'
          },
          width: '540',
          frame : true,
          method: 'GET',
          items: [{
              layout: 'column',
              xtype: 'container',
              anchor: '100%',
              items: [{
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [sumGrant]
                }, {
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [sumGrantConsume]
                }, {
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [grantResidualPayment]
                }]
            },{
              layout: 'column',
              xtype: 'container',
              anchor: '100%',
              items: [{
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [sumReceivable]
                }, {
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [sumReceivableConsume]
                }, {
                  columnWidth: .33,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [receivableResidualPayment]
                }]
            }],
          buttons: [closeButton]
        });
        
        residualPaymentFormPanel.render('sale_residual_payment');

        Ext.Ajax.request({
          url : '/finance/getResidualReceivableByCustomerIdStrategyId',
          success : function (result, request) {
            sumReceivable.setValue(Ext.JSON.decode(result.responseText).sum_amount);
            //如果是0才有必要考虑赠款的情况
            if(sumReceivable.getValue()==0){
              Ext.Ajax.request({
                url : '/finance/getResidualGrantByCustomerIdStrategyId',
                success : function (result, request) {
                  sumGrant.setValue(Ext.JSON.decode(result.responseText).sum_amount);
                  Ext.Ajax.request({
                    url : '/sale_order/getSumResidualPaymentConsumeByCustomerIdStrategyId',
                    success : function (result, request) {
                      sumGrantConsume.setValue(Ext.JSON.decode(result.responseText).sum_amount);
                      grantResidualPayment.setValue(sumGrant.getValue()-sumGrantConsume.getValue());
                    },
                    failure : function (result, request){
                      Ext.MessageBox.show({
                        title: '消息',
                        msg: "通讯失败，请从新操作",
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.WARNING
                      });
                    },
                    method : 'GET',
                    params : {
                      customer_id : <%=params[:customer_id]%>,
                      sales_strategy_id : <%=params[:sales_strategy_id]%>
                    }
                  });
                },
                failure : function (result, request){
                  Ext.MessageBox.show({
                    title: '消息',
                    msg: "通讯失败，请从新操作",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                },
                method : 'GET',
                params : {
                  customer_id : <%=params[:customer_id]%>,
                  sales_strategy_id : <%=params[:sales_strategy_id]%>
                }
              });
            }else{
              Ext.Ajax.request({
                url : '/sale_order/getSumResidualPaymentConsumeByCustomerIdStrategyId',
                success : function (result, request) {
                  sumReceivableConsume.setValue(Ext.JSON.decode(result.responseText).sum_amount);
                  receivableResidualPayment.setValue(sumReceivable.getValue()-sumReceivableConsume.getValue());
                },
                failure : function (result, request){
                  Ext.MessageBox.show({
                    title: '消息',
                    msg: "通讯失败，请从新操作",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                },
                method : 'GET',
                params : {
                  customer_id : <%=params[:customer_id]%>,
                  sales_strategy_id : <%=params[:sales_strategy_id]%>
                }
              });
            }            
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET',
          params : {
            customer_id : <%=params[:customer_id]%>,
            sales_strategy_id : <%=params[:sales_strategy_id]%>
          }
        });
      })
    </script>
  </head>
  <body>
    <div id="sale_residual_payment"></div>
  </body>
</html>